package com.sanjiahantang.youjiarole.module.money.views.impl;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sanjiahantang.plugin.pickerview.OptionsPickerView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.dialog.SendMessageDialog;
import com.sanjiahantang.youjiarole.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiarole.module.common.presenters.MeAccountPresenter;
import com.sanjiahantang.youjiarole.module.common.utils.CommonModelUtils;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;
import com.sanjiahantang.youjiarole.module.common.views.impl.SelectorActivity;
import com.sanjiahantang.youjiarole.module.join.beans.CertificationResponseBean;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加银行卡
 * Created by admin on 2017/10/28.
 */

public class MeAddBankActivity extends BaseMvpActivity<MeAccountView, MeAccountPresenter> implements MeAccountView, View.OnClickListener {
    private View fl_Left, selectBank;
    private TextView tvBankName,tvBankNo,tvHolder,tvIdCard,tvPhone,tvType,tvOpenBank,tvBankAddress;
    private View vBankName,vType,vBankAddress,btn_beSure;


    private String strBankName,strBankNo,strBankCode,strHolder,strPhone,strType,strOpenBank,strBankAddress,strProvince,strCity;
    private final int INTENT_BANK = 1,INTENT_TYPE = 2;
    private ArrayList<ConditionBean.SelectorData> bankList,typeList;
    private ConditionBean.SelectorData bankData,typeData;
    private OptionsPickerView pvOptions;

    private ArrayList<String> listProvince;
    private ArrayList<ArrayList<String>> listCity;

    @Override
    public MeAccountPresenter initPresenter() {
        return new MeAccountPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_add_bank);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("添加银行卡");
        initTypeList();
        initArea();

        tvBankName = (TextView) findViewById(R.id.tvBankName);
        tvBankNo = (TextView) findViewById(R.id.tvBankNo);
        tvHolder = (TextView) findViewById(R.id.tvHolder);
        tvIdCard = (TextView) findViewById(R.id.tvIdCard);
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvType = (TextView) findViewById(R.id.tvType);
        tvOpenBank = (TextView) findViewById(R.id.tvOpenBank);
        tvBankAddress = (TextView) findViewById(R.id.tvBankAddress);

        vBankName = findViewById(R.id.vBankName);
        vType = findViewById(R.id.vType);
        vBankAddress = findViewById(R.id.vBankAddress);
        btn_beSure = findViewById(R.id.btn_beSure);

        vBankName.setOnClickListener(this);
        vType.setOnClickListener(this);
        vBankAddress.setOnClickListener(this);
        btn_beSure.setOnClickListener(this);
    }

    private void initTypeList() {

        typeList = new ArrayList<>();
        ConditionBean.SelectorData type1 = new ConditionBean.SelectorData();
        type1.setDicName("对公");
        type1.setDicValue("0");

        ConditionBean.SelectorData type2 = new ConditionBean.SelectorData();
        type2.setDicName("个人");
        type2.setDicValue("1");

        typeList.add(type1);
        typeList.add(type2);
    }

    private void initArea(){
        CommonModelUtils.getAllArea($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] listArray = (Object[]) content;
                listProvince = (ArrayList<String>) listArray[0];
                listCity = (ArrayList<ArrayList<String>>) listArray[1];
                pvOptions = new OptionsPickerView.Builder($this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        strProvince = listProvince.get(options1);
                        strCity = listCity.get(options1).get(options2);
                        String tx = strProvince +  " " +  strCity;
                        tvBankAddress.setText(tx);

                    }
                })
                        .setTitleText("")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build();

                pvOptions.setPicker(listProvince, listCity);//三级选择器
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void onClick(View view) {
        KeyBoardUtils.closeKeybordOnly($this);
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.vBankName:
                if (bankList == null){
                    CommonModelUtils.findDicInfoByParentIdForCommon($this,CommonModelUtils.TYPE_BANK, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            if (content != null){
                                if (content instanceof List){
                                    bankList = (ArrayList<ConditionBean.SelectorData>) content;
                                    SelectorActivity.startActivity($this,bankList,"选择银行",INTENT_BANK);
                                }
                            }
                        }
                    });
                }else{
                    SelectorActivity.startActivity($this,bankList,"选择银行",bankData == null ? null : bankData.getDicValue(),INTENT_BANK);
                }

                break;
            case R.id.vType:
                SelectorActivity.startActivity($this,typeList,"选择类型",typeData == null ? null : typeData.getDicValue(),INTENT_TYPE);
                break;
            case R.id.vBankAddress:
                pvOptions.show();
                break;
            case R.id.btn_beSure:
                presenter.addBankCardInfo($this,
                        bankData,
                        tvBankNo.getText().toString(),
                        tvHolder.getText().toString(),
                        tvIdCard.getText().toString(),
                        tvPhone.getText().toString(),
                        typeData,
                        tvOpenBank.getText().toString(),
                        strProvince,
                        strCity,
                        new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        if (content != null){
                            finish();
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case INTENT_BANK:
                    bankData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (bankData != null){
                        tvBankName.setText(bankData.getDicName());
                    }
                    break;
                case INTENT_TYPE:
                    typeData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (typeData != null){
                        tvType.setText(typeData.getDicName());
                    }
                    break;
            }
        }
    }


}
